package com.yonyougov.cruxtestcase.testcase.context;

import com.yonyougov.cruxtestcase.component.ZWCDInvoke;
import com.yonyougov.cruxtestcase.component.sm.SmClient;
import com.yonyougov.cruxtestcase.component.uc.UcClient;
import com.yonyougov.cruxtestcase.component.uc.dto.UserDto;
import com.yonyougov.cruxtestcase.component.uc.dto.UserOfficeDto;
import com.yonyougov.cruxtestcase.dto.BaseResponseDto;
import com.yonyougov.cruxtestcase.testcase.context.dto.ContextCaseReqDto;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ContextCaseService {
    private final ZWCDInvoke zwcdInvoke;
    private final UcClient ucClient;
    private final SmClient smClient;
    private final Long organizationIdOfJX = 3285292485697456694L;
    private final Integer organizationCodeOfJX = 36;
    private final Long organizationIdOfNC = 3285292188347586215L;
    private final Integer organizationCodeOfNC = 3601;
    public ContextCaseService(UcClient ucClient, ZWCDInvoke zwcdInvoke, SmClient smClient) {
        this.ucClient = ucClient;
        this.zwcdInvoke = zwcdInvoke;
        this.smClient = smClient;
    }

    /**
     * 测试用例1
     * 在江西省下用9个9用户新建用户1，9个9 退出；用户1登陆，上下文的用户任职组织默认是江西省
     * @param baseDto
     * @return
     */
    public BaseResponseDto caseOne(ContextCaseReqDto baseDto) throws Exception {
        BaseResponseDto baseResponseDto = new BaseResponseDto();
        //获取baseUrl
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        //用9个9用户登陆
        String token = login(baseDto);
        //创建用户1
        String user01 ="用户1"+RandomUtils.nextLong(1,1000L);
        UserDto userDto = ucClient.saveUser(baseUrl, token, new UserDto(null,user01, "用户1", UserDto.OrgType.REGION, organizationIdOfJX,true,false));
        //9个9用户退出
        ucClient.logout(baseUrl,token);
        //用户1登陆
        baseDto.setUser(user01);
        baseDto.setPassword("1");
        token = login(baseDto);
        // 检查此时上下文组织是江西省
        Integer organizationCode = (Integer) smClient.contextValue(baseUrl, token, "organizationCode");
        if(organizationCode.equals(organizationCodeOfJX)){
            baseResponseDto.setSuccess(true);
        }else {
            baseResponseDto.setErrorMsg("用户2登录后此时上下文组织不是江西省");
        }
        return baseResponseDto;
    }

    /**
     * 测试用例2
     * 在江西省下用9个9用户新建用户2，给用户2任职南昌市，9个9 退出；用户2登陆，上下文的用户任职组织默认是江西省
     * @param baseDto
     * @return BaseResponseDto
     */
    public BaseResponseDto caseTwo(ContextCaseReqDto baseDto) throws Exception {
        BaseResponseDto baseResponseDto = new BaseResponseDto();

        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        // 登录9个9用户
        String token = login(baseDto);
        // 新建用户2 任职南昌市
        String user2Code = "用户2"+ RandomUtils.nextLong(1,1000);
        String user2Name = "用户2";
        ucClient.saveUser(baseUrl,token,new UserDto(null,user2Code,user2Name, UserDto.OrgType.REGION,organizationIdOfNC,true,false));
        // 9个9退出登录
        ucClient.logout(baseUrl,token);
        // 用户2登录
        baseDto.setUser(user2Code);
        baseDto.setPassword("1");
        token = login(baseDto);
        // 检查此时上下文组织是江西省
        Integer organizationCode = (Integer) smClient.contextValue(baseUrl, token, "organizationCode");
        if(organizationCode.equals(organizationCodeOfJX)){
            baseResponseDto.setSuccess(true);
        }else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("测试不通过:用户2登录后此时上下文组织不是江西省");
        }

        return baseResponseDto;
    }


    /**
     * 测试用例3
     * 在江西省下用9个9用户新建用户3，给用户3任职南昌市，9个9 退出；用户3登陆，切换任职组织为南昌市，用户3退出；用户3再次登陆，上下文的用户任职组织默认是南昌市
     * @param baseDto
     * @return BaseResponseDto
     */
    public BaseResponseDto caseThree(ContextCaseReqDto baseDto) throws Exception {
        BaseResponseDto baseResponseDto = new BaseResponseDto();

        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        // 登录9个9用户
        String token = login(baseDto);
        // 新建用户3 任职南昌市
        String user3Code = "用户3"+ RandomUtils.nextLong(1,1000);
        String user3Name = "用户3";
        ucClient.saveUser(baseUrl,token,new UserDto(null,user3Code,user3Name, UserDto.OrgType.REGION,organizationIdOfNC,true,false));
        // 9个9退出登录
        ucClient.logout(baseUrl,token);
        // 用户3登录
        baseDto.setUser(user3Code);
        baseDto.setPassword("1");
        token = login(baseDto);
        // 切换任职组织为南昌市
        Map<String,String> param = new HashMap<>();
        param.put("organizationId", String.valueOf(organizationIdOfNC));
        smClient.fillValue(baseUrl, token,param);
        // 用户3退出
        ucClient.logout(baseUrl,token);
        // 用户3再次登录
        baseDto.setUser(user3Code);
        baseDto.setPassword("1");
        token = login(baseDto);
        // 检查上下文的用户任职组织默认是南昌市
        Integer organizationCode = (Integer) smClient.contextValue(baseUrl, token, "organizationCode");
        if(organizationCode.equals(organizationCodeOfNC)){
            baseResponseDto.setSuccess(true);
        }else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("测试不通过: 用户3登录后此时上下文组织不是南昌市");
        }

        return baseResponseDto;
    }

    /**
     * 测试用例4
     * 在江西省下用9个9用户新建用户4，给用户4任职南昌市，9个9 退出；用户4登陆，切换任职组织为南昌市，用户4退出；9个9用户再次登陆，删除用户4的任职组织南昌市，退出；用户4再登陆，上下文的用户任职组织默认是江西省
     * @param baseDto
     * @return
     * @throws Exception
     */
    public BaseResponseDto caseFour(ContextCaseReqDto baseDto) throws Exception {
        BaseResponseDto baseResponseDto = new BaseResponseDto();

        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        // 登录9个9用户
        String token = login(baseDto);
        // 新建用户4 任职南昌市
        String user4Code = "用户4"+ RandomUtils.nextLong(1,1000);
        String user4Name = "用户4";
        UserDto userDto = ucClient.saveUser(baseUrl, token, new UserDto(null, user4Code, user4Name, UserDto.OrgType.REGION, organizationIdOfNC,true,false));
        // 9个9退出登录
        ucClient.logout(baseUrl,token);
        // 用户4再次登录
        ContextCaseReqDto contextCaseReqDto = new ContextCaseReqDto();
        contextCaseReqDto.setEnvName(baseDto.getEnvName());
        contextCaseReqDto.setUser(user4Code);
        contextCaseReqDto.setPassword("1");
        token = login(contextCaseReqDto);
        //切换任职组织为南昌市
        Map<String, String> param = new HashMap<>();
        param.put("organizationId", String.valueOf(organizationIdOfNC));
        smClient.fillValue(baseUrl,token,param);
        //用户4退出
        ucClient.logout(baseUrl,token);
        // 登录9个9用户
        token = login(baseDto);
        //删除用户4的任职组织南昌市
        userDto.setOrganization(organizationIdOfNC);
        UserOfficeDto userOfficeDto = ucClient.queryUserOfficeByUserAndOrg(baseUrl, token, userDto);
        ucClient.delUserOffice(baseUrl,token,userOfficeDto);
        //9个9退出
        ucClient.logout(baseUrl,token);
        //用户4再登陆
        token = login(contextCaseReqDto);
        // 检查此时上下文组织是江西省
        Integer organizationCode = (Integer) smClient.contextValue(baseUrl, token, "organizationCode");
        if(organizationCode.equals(organizationCodeOfJX)){
            baseResponseDto.setSuccess(true);
        }else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("测试不通过: 用户4登录后此时上下文组织不是江西省");
        }

        return baseResponseDto;
    }



    /**
     * 用户登录
     * @param baseDto 基础Dto
     * @return token
     */
    private String login(ContextCaseReqDto baseDto) throws Exception {

        //获取baseUrl
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        // 用户登陆
        BaseResponseDto ucBaseResponseDto = ucClient.login(baseUrl, baseDto.getUser(), baseDto.getPassword());
        String token = "";
        if(ucBaseResponseDto.getSuccess()==true){
            token = String.valueOf(ucBaseResponseDto.getData().get("token"));
        }

        if (StringUtils.isNotBlank(token)) {

        } else {
            throw new Exception("用户名或者密码错误，获取token失败");
        }
        return token;
    }


}
